package service;

import dao.RatingDao;
import model.Rating;

import java.sql.SQLException;
import java.util.List;

/**
 * 商品评分服务类
 */
public class RatingService {
    private RatingDao ratingDao = new RatingDao();
    
    /**
     * 添加或更新评分
     * @param user_id 用户ID
     * @param goods_id 商品ID
     * @param score 评分 (1-5)
     * @param comment 评论内容
     * @return 操作结果
     */
    public boolean rateGoods(int user_id, int goods_id, int score, String comment) {
        try {
            Rating rating = new Rating(user_id, goods_id, score, comment);
            // 检查是否已评分
            if (ratingDao.isRated(user_id, goods_id)) {
                // 已评分，执行更新
                ratingDao.updateRating(rating);
            } else {
                // 未评分，执行添加
                ratingDao.addRating(rating);
            }
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
    
    /**
     * 删除评分
     * @param user_id 用户ID
     * @param goods_id 商品ID
     * @return 操作结果
     */
    public boolean deleteRating(int user_id, int goods_id) {
        try {
            ratingDao.deleteRating(user_id, goods_id);
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
    
    /**
     * 检查用户是否已对商品评分
     * @param user_id 用户ID
     * @param goods_id 商品ID
     * @return 是否已评分
     */
    public boolean isRated(int user_id, int goods_id) {
        try {
            return ratingDao.isRated(user_id, goods_id);
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
    
    /**
     * 获取用户对商品的评分
     * @param user_id 用户ID
     * @param goods_id 商品ID
     * @return 评分对象
     */
    public Rating getRatingByUserIdAndGoodsId(int user_id, int goods_id) {
        try {
            return ratingDao.getRatingByUserIdAndGoodsId(user_id, goods_id);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }
    
    /**
     * 获取商品的所有评分
     * @param goods_id 商品ID
     * @return 评分列表
     */
    public List<Rating> getRatingsByGoodsId(int goods_id) {
        try {
            return ratingDao.getRatingsByGoodsId(goods_id);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }
    
    /**
     * 获取商品的平均评分
     * @param goods_id 商品ID
     * @return 平均评分
     */
    public double getAverageScoreByGoodsId(int goods_id) {
        try {
            return ratingDao.getAverageScoreByGoodsId(goods_id);
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }
    
    /**
     * 获取商品的评分数量
     * @param goods_id 商品ID
     * @return 评分数量
     */
    public int getRatingCountByGoodsId(int goods_id) {
        try {
            return ratingDao.getRatingCountByGoodsId(goods_id);
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }
} 